<?php
/**
 * @name 数据同步配置控制器
 */
namespace app\admin\controller;
use app\common\controller\AdminBase;
class Syncdata extends AdminBase {
    /**
     * @name 初始化
     */
    protected function _initialize(){
        //添加初始化方法
        $this->mod = model('admin/Config');
    }
    
    /**
     * @name 配置页
     */
    public function index(){
        $this->pageTitle = '基础接口配置';
        $list = $this->mod->getConfigList('sync');
        if(!$list['data']){
            $this->initSyncConf();
            $list = $this->mod->getConfigList('sync');
        }
        $this->assign('list',$list);
        $user_service = $this->mod->getConfigList('user_service','',1);
        if(!$user_service['data']){
            //添加默认数据
             $this->add_user_service();
             $user_service = $this->mod->getConfigList('user_service','',1);
        }
        $this->assign('user_service',$user_service['data'][0]);
        return $this->fetch();
    }
    /**
     * @name 查看详情
     */
    public function info(){
        $this->pageTitle = '查看详情';
        $info = $this->mod->getConfigById(decrypt_id($this->config_id));
        $this->assign('info',$info);
        return $this->fetch();
    }
    /**
     * @name 编辑
     */
    public function edit(){
        $this->pageTitle = '修改接口';
        $info = $this->mod->getConfigById(decrypt_id($this->config_id));
        $this->assign('info',$info);
        return $this->fetch();
    }
    /**
     * @name 检测API地址是否有效
     */
    public function checkApiUrl(){
        if(IS_AJAX){
            $data = [
                'url' => $this->value['url'],
                'data' =>arr2one($this->value['body']),
                'method' => $this->value['method'],
            ];
            $this->header = arr2one($this->value['header']);
            $re = $this->request($data['method'],$data['data'],$data['url']);
            if($re){
                return ['status'=>1,'data'=>$re];
            }
        }
        return ['status'=>0,'message'=>'接口地址不可用'];
    }
    /**
     * @name 保存修改 
     */
    public function save(){
        if(IS_AJAX){
            $this->config_id = decrypt_id($this->config_id);
            if($this->config_id){
                $data = [
                    'value' => [
                        'url' => $this->value['url'],
                        'data' =>arr2one($this->value['body']),
                        'header' =>arr2one($this->value['header']),
                        'method' => $this->value['method'],
                    ],
                    'config_id' => $this->config_id,
                    'status' => $this->value['url'] ? 1 :0
                ];
                if($this->mod->editConfig($data,2)){
                    system_log('修改接口【'.$this->mod->getConfigDescription($this->config_id).'】配置','admin_config',['key'=>'shielding_version_update']);
                    return ['status'=>1,'data'=>['info'=>'更新成功','jumpurl'=>U('admin/Syncdata/index')]];
                }
            }
        }
        return ['status'=>0,'message'=>'更新失败,请重试'];
    }
    private function initSyncConf(){
        $this->addUserDataConf();
        $this->addUserGiveConf();
        $this->addUserDataSyncConf();
        $this->addStreamStatusConf();
    }
    /**
     * @name 添加配置--获取用户数据
     */
    final private function addUserDataConf(){
        $data = [
            'type' => 'sync',
            'value' => [
                'url' => '',
                'data' => [],
                'header' => [
                    
                ],
                'method' => 'post'
            ],
            'description' => '获取用户数据接口',
            'key'   => 'user_data_get_conf',
            'status' => 0
        ];
        return $this->mod->addConfig($data,2);
    }
     /**
     * @name 添加配置--请求赠送礼物
     */
    final private function addUserGiveConf(){
        $data = [
            'type' => 'sync',
            'value' => [
                'url' => '',
                'data' => [],
                'header' => [
                    
                ],
                'method' => 'post'
            ],
            'description' => '请求赠送礼物',
            'key'   => 'user_give_gift_conf',
            'status' => 0
        ];
        return $this->mod->addConfig($data,2);
    }
     /**
     * @name 添加配置--同步用户数据
     */
    final private function addUserDataSyncConf(){
        $data = [
            'type' => 'sync',
            'value' => [
                'url' => '',
                'data' => [],
                'header' => [
                    
                ],
                'method' => 'post'
            ],
            'description' => '同步数据',
            'key'   => 'user_data_sync_conf',
            'status' => 0
        ];
        return $this->mod->addConfig($data,2);
    }
	 /**
     * @name 添加配置--推流状态回调
     */
    final private function addStreamStatusConf(){
        $data = [
            'type' => 'sync',
            'value' => [
                'url' => '',
                'data' => [],
                'header' => [
                    
                ],
                'method' => 'post'
            ],
            'description' => '直播状态回调',
            'key'   => 'stream_status_back_conf',
            'status' => 0
        ];
        return $this->mod->addConfig($data,2);
    }
     /**
     * @name 添加默认配置
     */
    final private function add_user_service(){
        $data = [
            'type' => 'user_service',
            'value' => [
                'params' => [],
                'header' => []
            ],
            'description' => '接口调用配置',
            'key'   => 'user_service_api',
        ];
        return $this->mod->addConfig($data,2);
    }
    /**
     * @name 添加默认配置
     */
    public function edit_user_service(){
        if(IS_AJAX){
            $this->config_id = decrypt_id($this->config_id);
            if($this->config_id){
                $data = [
                    'value' => [
                        'params' => arr2one($this->value['params']),
                        'header' => arr2one($this->value['header'])
                    ],
                    'config_id' => $this->config_id,
                ];
                if($this->mod->editConfig($data,2)){
                    return ['status'=>1,'data'=>['info'=>'更新成功','jumpurl'=>U('admin/Syncdata/index')]];
                }
            }
        }
        return ['status'=>0,'message'=>'更新失败,请重试'];
    }
}